package get.data;

import java.util.ArrayList;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

class TagNode {
	
	public TextView add;
	public TextView temp;
	public TextView light;
	public ImageView imagine;
}

public class NodeAdaptor extends BaseAdapter {
	
	private Activity activity;
	private ArrayList<myNode> nodeList = new ArrayList<myNode>();
	private int type; // 1 for list of temperatures
				  // 2 for list of powers

	public NodeAdaptor(Activity activity, int type){
			this.activity = activity;
			this.type = type;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return nodeList.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return nodeList.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		//returns the index of the node in Main.nodes array
		String  ip = nodeList.get(pos).GetAdd();
    	for(int i=0;i<Main.nodes.size();i++){
    		if(Main.nodes.get(i).GetAdd().compareTo(ip)==0)
    			return i;
    	}
    	return -1;
	}

	@Override
	public View getView (int pos, View convertView, ViewGroup list) {

		View element;

		if (convertView == null){
			
			LayoutInflater inflater = activity.getLayoutInflater();
			element = inflater.inflate(R.layout.view_parametres_list_element, null); 

			TagNode tagNode = new TagNode();
			tagNode.add = (TextView)element.findViewById(R.id.add); 
			tagNode.temp = (TextView)element.findViewById(R.id.param); 
			tagNode.imagine = (ImageView)element.findViewById(R.id.status); 
			element.setTag(tagNode); 
			
		} else {
			element = convertView;
		}
		
		TagNode tagNode = (TagNode)element.getTag();

		if(type == 1){
			String temp = URLresponse(nodeList.get(pos).GetTemp());

			tagNode.add.setText("IP: "+nodeList.get(pos).GetAdd());
			tagNode.temp.setText("Temp: "+temp);
			
			if(nodeList.get(pos).GetStatus())
				if(Float.parseFloat(temp.trim()) > Limits.lowhot)
					tagNode.imagine.setImageResource(R.drawable.hoton);
				else if(Float.parseFloat(temp.trim()) > Limits.lownormal)
					tagNode.imagine.setImageResource(R.drawable.normalon);
				else
					tagNode.imagine.setImageResource(R.drawable.freezeon);
			else 
				tagNode.imagine.setImageResource(R.drawable.off);
		}
		
		else if(type == 2){
			String light = URLresponse(nodeList.get(pos).GetLight());

			tagNode.add.setText("IP: "+nodeList.get(pos).GetAdd());
			tagNode.temp.setText("Light: "+light);
			
			if(nodeList.get(pos).GetStatus())
				tagNode.imagine.setImageResource(R.drawable.normalon);
			else 
				tagNode.imagine.setImageResource(R.drawable.off);
		}
		
		return element;
	}
	
	public static String URLresponse(String URL){
		
		HttpClient client = new DefaultHttpClient();
	    HttpGet getMethod=new HttpGet(URL);
	    String responseBody="";
	    try {
	    	ResponseHandler<String> responseHandler=new BasicResponseHandler();
	    	responseBody=client.execute(getMethod, responseHandler);
	    	System.out.println(responseBody);
	    	int idx = responseBody.indexOf("?>");
	    	responseBody = responseBody.substring(idx+2);
	    	idx = responseBody.indexOf('>');
	    	responseBody = responseBody.substring(idx+1);
	    	idx = responseBody.indexOf('<');
	    	responseBody = responseBody.substring(0,idx-1);
	    	return responseBody;
	  
	    }catch(Exception e){
	    	System.out.println(e);
	    	System.out.println(URL);
	    }
		return null;
		
	}
	
	public void addNodes ()
	{
		for(int i=0;i<Main.nodes.size();i++)
			System.out.println(Main.nodes.get(i));
		
		if(type == 1){ // temperature
			for(int i=0;i<Main.nodes.size();i++)
				if((Main.nodes).get(i).GetTemp()!=null)
					nodeList.add(Main.nodes.get(i));
		}
	   else if(type == 2){ // temperature
			for(int i=0;i<Main.nodes.size();i++)
				if((Main.nodes).get(i).GetLight()!=null)
					nodeList.add(Main.nodes.get(i));
	   }

		this.notifyDataSetChanged();
	}
}